// 文件预览
export function prevFile(item) {
	let path = item.accessoryUrl ? item.accessoryUrl : item.url;
	let name = item.accessoryName ? item.accessoryName: item.name;

	//文件路径
	var filePath = name;
	//获取后缀
	//获取最后一个.的位置
	var index = filePath.lastIndexOf(".");

	var ext = filePath.substring(index + 1);

	// 预览pdf
	if (ext == "pdf") {
		uni.navigateTo({
			url: `/pages/DJpreviewPDF/index?url=${path}&title=${name}`,
		});
	} else {
		uni.showLoading({
			title: '加载中……',
			mask: true
		})
		let zip = ['rar', '7z', 'zip'];
		let type = name.substring(name.lastIndexOf(".") + 1);
		if (zip.indexOf(type) > 0) {
			uni.showToast({
				title: '类型不支持预览',
				icon: 'error',
				mask: true,
				duration: 1500
			})
			uni.hideLoading();
			return;
		}
		console.log(type);
		let times = Date.parse(new Date()); //拼接时间戳
		uni.downloadFile({
			url: path + '?code=' + times,
			success: function (res) {
				uni.hideLoading();
				var filePath = res.tempFilePath;
				if (type === 'jpg' || type === 'jpeg' || type === 'png') {
					//如果是图片走这里
					uni.previewImage({
						current: 0,
						urls: [path],
					})
				} else {
					//如果是文件走这里
					uni.hideLoading();
					uni.openDocument({
						filePath: filePath,
						success: (res) => console.log('成功打开文档'),
						fail: (res) => {
							uni.showToast({
								icon: 'none',
								title: '文件打开失败',
								duration: 2000
							})
						}
					})
				}
			}
		});
	}
}